package com.example.administrator.student4.Adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.student4.R;

import org.json.JSONArray;
import org.json.JSONException;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.CharsetDecoder;

/**
 * Created by Administrator on 2016/12/12.
 */
public class SubjectAdapter extends BaseAdapter {
    Context context;
    JSONArray array;
    ViewHolder viewHolder;
    int deleteposition = 0,deleteid;
    Myhandler myhandler;
    public SubjectAdapter(Context context, JSONArray array) {
        this.context = context;
        this.array = array;
    }

    @Override
    public int getCount() {
        return array.length();
    }

    @Override
    public Object getItem(int position) {
        Object object = null;
        try {
            object= array.getJSONObject(position);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return object;
    }
    public void remove(int position){
        array.remove(position);
    }
    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        viewHolder = new ViewHolder();
        myhandler = new Myhandler();
        if(convertView==null){
            convertView = LayoutInflater.from(context).inflate(R.layout.subjectlv,null);
            viewHolder.subject = (TextView) convertView.findViewById(R.id.subject);
            viewHolder.teacher = (TextView) convertView.findViewById(R.id.teacher);
            viewHolder.term = (TextView) convertView.findViewById(R.id.term);
            viewHolder.delete = (Button) convertView.findViewById(R.id.delete);
            convertView.setTag(viewHolder);
        }
        else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        try {
            viewHolder.subject.setText(array.getJSONObject(position).getString("subject"));
            viewHolder.teacher.setText(array.getJSONObject(position).getString("teacher"));
            viewHolder.term.setText(array.getJSONObject(position).getString("term"));
            viewHolder.delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    new Thread(){
                        @Override
                        public void run() {
                            super.run();
                            try {
                                deleteposition = position;
                                deleteid = array.getJSONObject(position).getInt("id");
                                String path = "http://10.0.2.2:8080/andorid/subject?request=delete&id="+deleteid;
                                URL url = new URL(path);
                                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                                connection.connect();
                                if(connection.getResponseCode()==200) connection.getInputStream();
                                myhandler.sendEmptyMessage(0x111);
                            } catch (JSONException | IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }.start();
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return convertView;
    }
    class ViewHolder{
        TextView subject,teacher,term;
        Button delete;
    }
    class Myhandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what ==0x111){
                remove(deleteposition);
                Toast.makeText(context,"删除成功",Toast.LENGTH_SHORT).show();
                notifyDataSetChanged();
            }
        }
    }
}
